package org.example.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.OrganizationStructureMapper;
import org.example.model.common.AppHttpCodeEnum;
import org.example.model.common.ResponseResult;
import org.example.model.user.dtos.OSContainsNameDto;
import org.example.model.user.dtos.OrganizationStructureDto;
import org.example.model.user.dtos.UserIdentityDto;
import org.example.model.user.entities.OrganizationStructure;
import org.example.model.user.vos.AddOrganizationStructureVo;
import org.example.model.user.vos.SearchOsVo;
import org.example.service.user.OrganizationStructureService;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 组织架构服务层实现
 */
@Service
@Transactional
@Slf4j
public class OrganizationStructureServiceImpl extends ServiceImpl<OrganizationStructureMapper, OrganizationStructure> implements OrganizationStructureService {

    @Autowired
    private OrganizationStructureMapper organizationStructureMapper;

    /**
     * 添加一条组织架构信息
     */
    @Override
    public ResponseResult<Boolean> InsertOrganizationStructure(AddOrganizationStructureVo addOrganizationStructureVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (addOrganizationStructureVo == null) {
            // 缺少参数
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        OrganizationStructure organizationStructure = new OrganizationStructure();
        BeanUtils.copyProperties(addOrganizationStructureVo, organizationStructure);
        this.HandleOrganizationStructureBasic(organizationStructure);
        Boolean flag = organizationStructureMapper.insert(organizationStructure) > 0;
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    //处理添加组织架构信息时共有信息的处理
    private void HandleOrganizationStructureBasic (OrganizationStructure organizationStructure) {
        // 获取进程中的用户数据
        UserIdentityDto userIdentityDto = UserIdThreadLocalUtils.getUser();
        if (organizationStructure.getId() == 0) {
            organizationStructure.setCreateUserId(userIdentityDto.getId());
            organizationStructure.setCreateTime(LocalDateTime.now());
            organizationStructure.setIsDeleted(false);
        }
        organizationStructure.setUpdateUserId(userIdentityDto.getId());
        organizationStructure.setUpdateTime(LocalDateTime.now());
    }

    /**
     * 根据id修改一条组织架构信息
     */
    @Override
    public ResponseResult<Boolean> UpdateOrganizationStructureById(AddOrganizationStructureVo addOrganizationStructureVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (addOrganizationStructureVo == null || addOrganizationStructureVo.getId() <= 0) {
            // 缺少参数 组织架构信息id
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        OrganizationStructure organizationStructure = organizationStructureMapper.selectById(addOrganizationStructureVo.getId());
        BeanUtils.copyProperties(addOrganizationStructureVo, organizationStructure);
        this.HandleOrganizationStructureBasic(organizationStructure);
        Boolean flag = organizationStructureMapper.updateById(organizationStructure) > 0;
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 根据id删除一条组织架构信息
     */
    @Override
    public ResponseResult<Boolean> DeleteOrganizationStructureById(int id) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (id <= 0) {
            // 缺少参数 组织架构信息id
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        OrganizationStructure organizationStructure = organizationStructureMapper.selectById(id);
        organizationStructure.setIsDeleted(true);
        this.HandleOrganizationStructureBasic(organizationStructure);
        Boolean flag = organizationStructureMapper.updateById(organizationStructure) > 0;
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 获取组织架构树
     */
    @Override
    public ResponseResult<List<OrganizationStructureDto>> SelectOrganizationStructureList() {
        LambdaQueryWrapper<OrganizationStructure> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrganizationStructure::getParentId,0)
                .eq(OrganizationStructure::getIsDeleted,false);
        // 获取所有的父节点
        List<OrganizationStructure> organizationStructureList = organizationStructureMapper.selectList(queryWrapper);
        List<OrganizationStructureDto> organizationStructureDtoList = new ArrayList<>();
        OrganizationStructureDto organizationStructureDto = null;
        for (OrganizationStructure organizationStructure : organizationStructureList) {
            organizationStructureDto = new OrganizationStructureDto();
            BeanUtils.copyProperties(organizationStructure, organizationStructureDto);
            organizationStructureDto = this.SelectOrganizationStructureById(organizationStructureDto);
            organizationStructureDtoList.add(organizationStructureDto);
        }
        ResponseResult<List<OrganizationStructureDto>> result = new ResponseResult<>();
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), organizationStructureDtoList, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 根据id获取组织架构详情
     */
    private OrganizationStructureDto SelectOrganizationStructureById(OrganizationStructureDto organizationStructureDto) {
        List<OrganizationStructureDto> organizationStructureDtoList = this.HandleSonNodeList(organizationStructureDto.getId());
        if (organizationStructureDtoList.size() == 0) {
            return organizationStructureDto;
        }
        for (OrganizationStructureDto structureDto : organizationStructureDtoList) {
            this.SelectOrganizationStructureById(structureDto);
        }
        organizationStructureDto.setOrganizationStructureList(organizationStructureDtoList);
        return organizationStructureDto;
    }

    //根据父id获取子列表
    private List<OrganizationStructureDto> HandleSonNodeList(int parentId) {
        LambdaQueryWrapper<OrganizationStructure> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrganizationStructure::getParentId, parentId)
                .eq(OrganizationStructure:: getIsDeleted, false);
        List<OrganizationStructure> sonNodeList = organizationStructureMapper.selectList(queryWrapper);
        List<OrganizationStructureDto> organizationStructureDtoList = new ArrayList<>();
        OrganizationStructureDto organizationStructureDto = null;
        for (OrganizationStructure organizationStructure : sonNodeList) {
            organizationStructureDto = new OrganizationStructureDto();
            BeanUtils.copyProperties(organizationStructure, organizationStructureDto);
            organizationStructureDtoList.add(organizationStructureDto);
        }
        return organizationStructureDtoList;
    }

    /**
     * 根据id获取组织架构名称
     */
    @Override
    public String SelectStructureNameById(int id) {
        return organizationStructureMapper.selectById(id).getName();
    }

    /**
     * 根据组织架构类型获取组织架构列表
     */
    @Override
    public ResponseResult<List<OSContainsNameDto>> SelectOrganizationStructureListByType(SearchOsVo searchOsVo) {
        ResponseResult<List<OSContainsNameDto>> result = new ResponseResult<>();
        // 参数审查
        if (searchOsVo == null) {
            // 缺少参数 组织架构信息id
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        LambdaQueryWrapper<OrganizationStructure> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrganizationStructure::getType, searchOsVo.getType())
                .eq(OrganizationStructure:: getIsDeleted, false);
        List<OrganizationStructure> organizationStructureList = organizationStructureMapper.selectList(queryWrapper);
        List<OSContainsNameDto> osNameList = new ArrayList<>();
        OSContainsNameDto osContainsNameDto = null;
        for (OrganizationStructure organizationStructure : organizationStructureList) {
            osContainsNameDto = new OSContainsNameDto();
            BeanUtils.copyProperties(organizationStructure, osContainsNameDto);
            osNameList.add(osContainsNameDto);
        }
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), osNameList, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }
}
